package com.lamda.zhekou.config;

import com.lamda.zhekou.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.ErrorPage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.boot.context.embedded.MultipartConfigFactory;

import javax.servlet.MultipartConfigElement;

/**
 * WebServer配置
 */
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private LoginInterceptor loginInterceptor;

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }



    /**
     * @return
     */
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory multipartConfigFactory = new MultipartConfigFactory();
        multipartConfigFactory.setMaxFileSize("128MB");
        multipartConfigFactory.setMaxRequestSize("128MB");
        return multipartConfigFactory.createMultipartConfig();
    }

    @Bean
    public InternalResourceViewResolver getViewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/pages/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /**
         *
         <mvc:resources mapping="/resources/**" location="/WEB-INF/pages/"/>
         <mvc:resources mapping="/css/**" location="/WEB-INF/pages/css/"/>
         <mvc:resources mapping="/js/**" location="/WEB-INF/pages/js/"/>
         <mvc:resources mapping="/img/**" location="/WEB-INF/pages/img/"/>
         <mvc:resources mapping="/ajax/**" location="/WEB-INF/pages/ajax/"/>
         <mvc:resources mapping="/fonts/**" location="/WEB-INF/pages/fonts/"/>
         */
//        registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/pages/");
//        registry.addResourceHandler("/WEB-INF/pages/**").addResourceLocations("/WEB-INF/pages/");
        registry.addResourceHandler("/css/**").addResourceLocations("/WEB-INF/pages/css/");
        registry.addResourceHandler("/js/**").addResourceLocations("/WEB-INF/pages/js/");
        registry.addResourceHandler("/image/**").addResourceLocations("/WEB-INF/pages/image/");
//        registry.addResourceHandler("/ajax/**").addResourceLocations("/WEB-INF/pages/ajax/");
        registry.addResourceHandler("/fonts/**").addResourceLocations("/WEB-INF/pages/fonts/");
        registry.addResourceHandler("/bootstrap/**").addResourceLocations("/WEB-INF/pages/bootstrap/");

    }

    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {
        return new EmbeddedServletContainerCustomizer() {
            /**
             * Customize the specified {@link org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer}.
             *
             * @param container the container to customize
             */
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
                container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notFound"));
                container.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/serverError"));
                container.addErrorPages(new ErrorPage(HttpStatus.SERVICE_UNAVAILABLE, "/serverError"));
                container.addErrorPages(new ErrorPage(HttpStatus.HTTP_VERSION_NOT_SUPPORTED, "/serverError"));
                container.addErrorPages(new ErrorPage(HttpStatus.METHOD_NOT_ALLOWED, "/serverError"));

            }
        };
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        super.addInterceptors(registry);
        registry.addInterceptor(this.loginInterceptor);

    }


}